//
//  NYSTipsView.m
//  NtoccDriverNew
//
//  Created by niyongsheng on 2021/8/16.
//  Copyright © 2021 Arpa. All rights reserved.
//

#import "NYSTipsView.h"
#import "WSScrollLabel.h"

#define NYS_RealValue(x)          x*(ScreenW/375.0)
@interface NYSTipsView ()
{
    UIView *_bgV;
}

@end

@implementation NYSTipsView

+ (void)showTipsWithContent:(NSString *)content {
    [self showTipsWithTitle:@"提醒:"
                    content:content
                actionTitle:@"关于"
                  direction:NYSTipsInDirectionDown
           offsetFromCenter:UIOffsetMake(0, 0)
                   duration:2
                     onView:[[[UIApplication sharedApplication] delegate] window]
   actionButtonClickedBlock:nil
    closeButtonClickedBlock:nil];
}

+ (void)showTipsWithTitle:(NSString *)title
                  content:(NSString *)content
              actionTitle:(NSString *)actionTitle
    infoButtonClickedBlock:(void(^)(void))actionButtonClickedBlock {
    [self showTipsWithTitle:title
                    content:content
                actionTitle:actionTitle
                  direction:NYSTipsInDirectionDown
           offsetFromCenter:UIOffsetMake(0, 0)
                   duration:5.0
                     onView:[[[UIApplication sharedApplication] delegate] window]
   actionButtonClickedBlock:actionButtonClickedBlock
    closeButtonClickedBlock:nil];
}

+ (void)showTipsWithTitle:(NSString *)title
                  content:(NSString *)content
              actionTitle:(NSString *)actionTitle
    infoButtonClickedBlock:(void(^)(void))actionButtonClickedBlock
  closeButtonClickedBlock:(void(^)(void))closeButtonClickedBlock {
    [self showTipsWithTitle:title
                    content:content
                actionTitle:actionTitle
                  direction:NYSTipsInDirectionDown
           offsetFromCenter:UIOffsetMake(0, 0)
                   duration:MAXFLOAT
                     onView:[[[UIApplication sharedApplication] delegate] window]
   actionButtonClickedBlock:actionButtonClickedBlock
    closeButtonClickedBlock:closeButtonClickedBlock];
}

+ (void)showTipsWithTitle:(NSString *)title
                  content:(NSString *)content
              actionTitle:(NSString *)actionTitle
                direction:(NYSTipsInDirection)direction
         offsetFromCenter:(UIOffset)offsetFromCenter
                 duration:(float)dutation
                   onView:(UIView *)view
 actionButtonClickedBlock:(void(^)(void))actionButtonClickedBlock
  closeButtonClickedBlock:(void(^)(void))closeButtonClickedBlock {
    [self showTipsWithTitle:title
                    content:content
                actionTitle:actionTitle
                  direction:NYSTipsInDirectionDown
           offsetFromCenter:UIOffsetMake(0, 0)
                   duration:MAXFLOAT
                     onView:[[[UIApplication sharedApplication] delegate] window]
                        tag:0
   actionButtonClickedBlock:actionButtonClickedBlock
    closeButtonClickedBlock:closeButtonClickedBlock];
}

+ (void)showTipsWithTitle:(NSString *)title
                  content:(NSString *)content
              actionTitle:(NSString *)actionTitle
                direction:(NYSTipsInDirection)direction
         offsetFromCenter:(UIOffset)offsetFromCenter
                 duration:(float)dutation
                   onView:(UIView *)view
                      tag:(NSInteger)tag
 actionButtonClickedBlock:(void(^)(void))actionButtonClickedBlock
  closeButtonClickedBlock:(void(^)(void))closeButtonClickedBlock {
    
    // bg view
    UIView *bgV = [UIView new];
    bgV.tag = tag;
    bgV.layer.cornerRadius = 2.0f;
    bgV.layer.masksToBounds = YES;
    [bgV setBackgroundColor:[UIColor colorWithHexString:@"#FFFF80"]];
    [view addSubview:bgV];
    
    // label
    UILabel *label = [UILabel new];
    label.text = title;
    label.textColor = [UIColor redColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont boldSystemFontOfSize:14];
    [bgV addSubview:label];
    
    WSScrollLabel *contentLabel = [WSScrollLabel new];
    contentLabel.text = content;
    contentLabel.velocity = 10;
    contentLabel.textColor = [UIColor darkGrayColor];
    contentLabel.textFont = [UIFont systemFontOfSize:13];
    [bgV addSubview:contentLabel];
    
    UIButton *actionBtn = [UIButton new];
    actionBtn.layer.cornerRadius = 4.0f;
    actionBtn.layer.masksToBounds = YES;
    actionBtn.titleLabel.textColor = [UIColor whiteColor];
    actionBtn.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    [actionBtn setTitle:actionTitle forState:UIControlStateNormal];
    actionBtn.backgroundColor = Theme_ColorValue;
    [actionBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        if (actionButtonClickedBlock) {
            actionButtonClickedBlock();
        }
    }];
    [bgV addSubview:actionBtn];
    
    UIButton *closeBtn = [UIButton new];
    [closeBtn setImage:[UIImage imageNamed:@"tip_close_icon"] forState:UIControlStateNormal];
    [closeBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        [UIView animateWithDuration:0.5f animations:^{
            bgV.alpha = 0;
        } completion:^(BOOL finished) {
            [bgV removeFromSuperview];
        }];
        if (closeButtonClickedBlock) {
            closeButtonClickedBlock();
        }
    }];
    [bgV addSubview:closeBtn];
    
    // 设置背景view的约束
    [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(ScreenW*0.95, NYS_RealValue(40)));
        
        switch (direction) {
            case NYSTipsInDirectionUp: {
                make.centerX.mas_equalTo(bgV.superview.mas_centerX);
                make.top.mas_equalTo(bgV.superview.mas_top).mas_offset(-NYS_RealValue(70));
            }
                break;
                
            case NYSTipsInDirectionDown: {
                make.centerX.mas_equalTo(bgV.superview.mas_centerX);
                make.bottom.mas_equalTo(bgV.superview.mas_bottom).mas_offset(NYS_RealValue(70));
            }
                break;
                
            case NYSTipsInDirectionLeft: {
                make.right.mas_equalTo(bgV.superview.mas_left);
                make.centerY.mas_equalTo(bgV.superview.mas_centerY);
            }
                break;
                
            case NYSTipsInDirectionRight: {
                make.left.mas_equalTo(bgV.superview.mas_right);
                make.centerY.mas_equalTo(bgV.superview.mas_centerY);
            }
                break;
                
            default:
                break;
        }
    }];
    
    // 设置label的约束
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label.superview.mas_left).mas_offset(10);
        make.centerY.mas_equalTo(label.superview.mas_centerY);
    }];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(closeBtn.superview.mas_right).mas_offset(0);
        make.centerY.mas_equalTo(closeBtn.superview.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    [actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(actionBtn.superview.mas_top).mas_offset(8);
        make.width.mas_equalTo(45);
        make.right.mas_equalTo(closeBtn.mas_left).mas_offset(0);
        make.centerY.mas_equalTo(actionBtn.superview.mas_centerY);
    }];
    
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label.mas_right).mas_offset(5);
        make.size.mas_equalTo(CGSizeMake(ScreenW*0.45, 30));
        make.centerY.mas_equalTo(contentLabel.superview.mas_centerY);
    }];
    
    [bgV.superview layoutIfNeeded];
    
    [bgV mas_updateConstraints:^(MASConstraintMaker *make) {
        switch (direction) {
            case NYSTipsInDirectionUp: {
                make.top.mas_equalTo(bgV.superview.mas_top).mas_offset(20 + NavHeight + offsetFromCenter.horizontal);
            }
                break;
                
            case NYSTipsInDirectionDown: {
                make.bottom.mas_equalTo(bgV.superview.mas_bottom).mas_offset(-20 - TabBarHeight + offsetFromCenter.vertical);
            }
                break;
                
            case NYSTipsInDirectionLeft: {
                make.right.mas_equalTo(bgV.superview.mas_left).mas_offset(ScreenW * 0.875 + offsetFromCenter.horizontal);
            }
                break;
                
            case NYSTipsInDirectionRight: {
                make.left.mas_equalTo((ScreenW * 0.25)/2);
            }
                break;
                
            default:
                break;
        }
    }];
    
    [UIView animateWithDuration:0.77 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        [bgV.superview layoutIfNeeded];
    } completion:^(BOOL finished) {
        // 自动移除
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(dutation * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:1.0f animations:^{
                bgV.alpha = 0;
            } completion:^(BOOL finished) {
                [bgV removeFromSuperview];
            }];
        });
    }];
    
    if (tag != 0) {
        bgV.alpha = 0;
    }
}

@end
